import { Component, OnInit, ElementRef } from '@angular/core';
import { TokenService } from 'src/app/core/service/token.service';
import { NzMessageService } from 'ng-zorro-antd';
import { LoginService } from 'src/app/core/service/login.service';
import { AuthService } from 'src/app/core/auth/auth.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-user-token',
  templateUrl: './user-token.component.html',
  styleUrls: ['./user-token.component.scss']
})
export class UserTokenComponent implements OnInit {
  // 时间选择器禁用日期 禁用比当天时间低的；
  showDateValue = null;
  startValue = null;
  todayValue = new Date();
  tokenContent: string;
  tokenMark = false; // 标识token已存在
  copyValue = '12345';
  disabledStartDate = (startValue: Date): boolean => { // 禁用小于当前时间
    if (!startValue) {
      return false;
    }
    return startValue.getTime() < this.todayValue.getTime();
  }


  constructor(
    private tokenService: TokenService,
    private message: NzMessageService,
    private loginservice: LoginService,
    private authService: AuthService,
    private router: Router,
    private elementRef: ElementRef
  ) {

  }

  ngOnInit() {
    this.getToken();
  }

  onChange(result: Date): void {
    console.log('Selected Time2: ', result);
    this.showDateValue = result;
  }

  // data-picker 时间选择器
  today() {
    this.showDateValue = new Date();
  }
  lastDay() {
    let lastDay = (new Date()).getDate() + 90;
    let day = (new Date()).setDate(lastDay);
    this.showDateValue = new Date(day);
    // 禁用大于选择的时间/90天后的时间
    this.disabledStartDate = (startValue: Date): boolean => {
      return startValue.getTime() > this.showDateValue.getTime();
    };
  }

  // 生成token ，新增
  addToken() {
    console.log('showDate===>', this.showDateValue);
    let postData = {
      expirationTime: this.showDateValue
    }
    this.tokenService.addToken(postData).subscribe(params => {
      console.log("test==>", params);
      if (params.status === 1) {
        this.tokenContent = params.data.tokenId;
        this.tokenMark = true;
      }
    });
  }
  // 重置token
  refreshToken() {
    this.tokenService.refreshToken().subscribe(params => {
      if (params.status === 1) {
        this.tokenContent = params.data.tokenId;
        // 退出
        // this.logout();
      }
    });
  }
  // 更新时间
  refreshTokenTime() {
    let postData = {
      expirationTime: this.showDateValue
    }
    this.tokenService.refreshTokenTime(postData).subscribe(params => {
   
      if (params.status === 1) {
        // this.tokenContent = params.data.tokenId;
        this.createMessage('success', '成功更新到期时间');
      }
    });
  }
  // 查询是否已经生成token
  getToken() {
    this.tokenService.getTokenInfo().subscribe(params => {
      if (params.status === 1) {
        this.tokenMark = true;
        this.tokenContent = params.data.tokenId;
        let date = new Date();
        this.showDateValue = new Date(date.setTime(params.data.expirationTime));
        console.log('time', new Date(params.data.expirationTime));
      }
    });
  }
  // 删除
  deleteToken() {

    this.tokenService.deleteToken().subscribe(params => {
      if (params.status === 1) {
        this.createMessage('success', '成功删除');
        this.showDateValue = null;
        this.tokenMark = false;
      }
    });
  }

  createMessage(type: string, content?: string): void {
    this.message.create(type, `${content}`);
  }

  confirm() {
    this.deleteToken();
  }
  cancel() {

  }
  // 退出返回到登录页面
  logout() {
    this.loginservice.logout().subscribe(() => {
      this.authService.clearUserInfo();
      this.router.navigate(['']);
    });
  }
  // 复制token
  copyToken() {
    const copyEl = this.elementRef.nativeElement.querySelector('#copyInput');
    const range = document.createRange();
    range.selectNode(copyEl);
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);
    document.execCommand('copy');
    this.createMessage('success', '复制成功');
  }
}
